<template>
  <div class="jeecg-basic-table jeecg-basic-table-form-container">
    <a-form @keyup.enter.native="searchQuery" style="width: 100%">
      <a-row :gutter="10" type="flex">
        <a-col flex="160px">
          <a-form-item label="区域" :label-col="{ style: { width: '50px' } }" :wrapper-col="{ span: 18 }">
            <JDictSelectTag
              v-model:value="queryParam.regions"
              mode="multiple"
              dictCode="sys_depart@authDepart"
              placeholder="全部"
              :maxTagCount="0"
              :getPopupContainer="(node) => node.parentNode"
              :dropdownStyle="{ width: '150px', overflow: 'auto' }"
              :dropdownMatchSelectWidth="false"
            />
          </a-form-item>
        </a-col>

        <a-col flex="330px">
          <a-form-item label="日期" :label-col="{ style: { width: '50px' } }" >
            <a-date-picker
              v-model:value="queryParam.beginDate"
              :disabled-date="disabledStartDate"
              valueFormat="YYYY-MM-DD"
              placeholder=""
              style="width: 123px"
            />
            ~
            <a-date-picker
              v-model:value="queryParam.endDate"
              :disabled-date="disabledEndDate"
              valueFormat="YYYY-MM-DD"
              placeholder=""
              style="width: 123px"
            />
          </a-form-item>
        </a-col>
        <a-col flex="125px">
          <a-form-item label="类型" :label-col="{ style: { width: '50px' } }" >
            <a-select v-model:value="queryParam.sflx" id="sflx" class="w80" placeholder="全部" :showArrow="false" :allowClear="true">
              <a-select-option value="1">收入</a-select-option>
              <a-select-option value="2">支出</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col flex="300px">
          <a-form-item label="项目" :label-col="{ style: { width: '50px' } }" >
            <Cascader v-model:value="cascader.ids" :options="cascader.options" placeholder="请选择" stringToNumber @change="cooperatorSelect" :show-search="{ filter }"/>
          </a-form-item>
        </a-col>
        <a-col flex="auto" style="float:right; width:150px; ">
          <span style="float: right;overflow: hidden;" class="table-page-search-submitButtons">
            <a-button type="primary" @click="searchQuery" preIcon="ant-design:search" class="mr-8px">查询</a-button>
            <a-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined" color="success"> 新增</a-button>
          </span>
        </a-col>
      </a-row>
    </a-form>
  </div>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :onRowClick="handleClick" :onRowDbClick="handleDblclick" :rowClassName="rowClassName">
      <!--插槽:table标题-->
      <!--template #tableTitle>
        <a-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined"> 新增</a-button>
        <a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportXls"> 导出</a-button>
        <j-upload-button type="primary" preIcon="ant-design:import-outlined" @click="onImportXls">导入</j-upload-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined"></Icon>
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button>批量操作
            <Icon icon="mdi:chevron-down"></Icon>
          </a-button>
        </a-dropdown>
      </template-->
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
    </BasicTable>

    <!-- 表单区域 -->
    <AccountModal @register="registerModal" @success="handleSuccess"></AccountModal>
  </div>
</template>

<script lang="ts" name="account-list" setup>
  import { ref, computed, unref, reactive, toRaw, onMounted, nextTick } from "vue";
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { columns } from './Account.data';
  import { list, deleteOne, coopSelect } from './Account.api';
  import { Cascader } from 'ant-design-vue';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import AccountModal from './modules/AccountModal.vue';
  import moment from 'dayjs'
  
  //初始化 -------------------------------------------------------------
  //定义查询表单，查询条件
  const queryParam = reactive({
    regions: [],
    sflx: '',
    beginDate : moment().subtract( 3, 'months').format('YYYY-MM-01'),
    endDate: moment().add( 0, 'months').format('YYYY-MM-DD'),
    coopId: ''
  });

  /* 日期参数 */
  function disabledStartDate(beginDate) {
    const endDate = queryParam.endDate
    if (!beginDate || !endDate) {
      return false
    }
    return beginDate.valueOf() > moment(endDate).valueOf()
  }

  function disabledEndDate(endDate) {
    const beginDate = queryParam.beginDate
    if (!endDate || !beginDate) {
      return false
    }
    return moment(beginDate).valueOf() > endDate.valueOf()
  }

  const cascader = reactive<any>({
    ids: [],
    options: []
  });

  function cooperatorSelect(value) {
    //console.info("coopOptions", value, cascader.ids)
    if (!value && value.length < 3) {
      return false
    }
    queryParam.coopId = value[2];
  }

  const filter = (inputValue: string, path) => {
    return path.some(option => option.label.toLowerCase().indexOf(inputValue.toLowerCase()) > -1);
  }

  //注册model
  const [registerModal, { openModal }] = useModal();
  //注册table数据
  const { prefixCls, tableContext } = useListPage({
    tableProps: {
      api: list,
      columns: columns,
      canResize: true,
      showTableSetting: false,
      showIndexColumn: true, //行号
      useSearchForm: false,
      actionColumn: {
        width: 100,
      },
      beforeFetch: (params) => Object.assign(params, unref(queryParam)),
    },
  });

  const [registerTable, { reload, setProps }, { selectedRowKeys }] = tableContext;

  /**
   * 查询
   */
  function searchQuery() {
    setProps({searchInfo:toRaw(queryParam)});
    reload()
  }

  /**
   * 新增
   */
  function handleAdd() {
    openModal(true, {
      isUpdate: false,
      showFooter: true,
    });
  }
  /**
   * 编辑事件
   */
  function handleEdit() {
    let record = { id: selectRows.id };
    openModal(true, {
      record,
      isUpdate: true,
      showFooter: true,
    });
  }

  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteOne({ id: record.id }, reload);
  }
  /**
   * 成功回调
   */
  function handleSuccess({ isUpdate, values }) {
    reload();
  }
  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        label: '编辑',
        onClick: handleEdit.bind(null, record),
      },
      {
        label: '删除',
        popConfirm: {
          color: 'error',
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record),
        },
      },
    ];
  }
  //双击行
  function handleDblclick() {
    handleEdit();
  }

  //单击行
  let selectRows = reactive<any>({ id: '' });
  function handleClick(record) {
    selectRows.id = record.id;
  }

  function rowClassName(record, index) {
    return record.id === selectRows.id ? 'row-selected' : '';
  }

  onMounted(async () => {
    await nextTick()
    let res = await coopSelect()
    Object.assign(cascader.options, res)
  });
</script>
<style scoped></style>
